#include <widget.h>
#include <ui_widget.h>

Widget::Widget(QWidget *parent)
{
    if (c.getSize() == 0) {
        curFigure = 'S';
    } else {
        curFigure = c.getShapeAt(0)->type();
//        curFigure = c.cont.at(0)->type();
    }
    setFixedSize(500, 500);

    ui = new Ui::Widget();
    ui->setupUi(this);

    scene = new QGraphicsScene(this);
    ui->graphicsView->resize(350, 350);
    scene->setSceneRect(0,0,350,350);
    ui->graphicsView->setScene(scene);
    ui->graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);


    QPushButton* circleBtn = ui->circleButton;
    QPushButton* rectangleBtn = ui->rectangeButton;
    QPushButton* squareBtn = ui->squareButton;

    QPushButton* saveBtn = ui->saveButton;
    QPushButton* loadBtn = ui->loadButton;

    QPushButton* rotateToRightBtn = ui->rotateToRight;
    QPushButton* rotateToLeftBtn = ui->rotateToLeft;

    connect(circleBtn, SIGNAL(clicked(bool)), this, SLOT(onCirclebuttonClicked()));
    connect(rectangleBtn, SIGNAL(clicked(bool)), this, SLOT(onRectanglebuttonClicked()));
    connect(squareBtn, SIGNAL(clicked(bool)), this, SLOT(onSquarebuttonClicked()));
    connect(loadBtn, SIGNAL(clicked(bool)), this, SLOT(onLoadbuttonClicked()));
    connect(saveBtn, SIGNAL(clicked(bool)), this, SLOT(onSavebuttonClicked()));
    connect(rotateToRightBtn, SIGNAL(clicked(bool)), this, SLOT(onRotateToRightButtonClicked()));
    connect(rotateToLeftBtn, SIGNAL(clicked(bool)), this, SLOT(onRotateToLeftButtonClicked()));
}



Widget::~Widget()
{
    for (int i = 0; i < c.getSize(); i++) {
        if (c.getShapeAt(i) != nullptr) {
            delete c.getShapeAt(i);
        }
    }
    if (ui != nullptr) {
        delete ui;
    }
    if (scene != nullptr) {
        scene->clear();
        delete scene;
    }

}

void Widget::onSavebuttonClicked()
{
    c.printShapesToFile("savedata");
}

void Widget::onLoadbuttonClicked()
{
    c.getDataFromFile("savedata.txt");
    showAllShapes();
}

void Widget::onSquarebuttonClicked()
{
    curFigure = 'S';
}

void Widget::onCirclebuttonClicked()
{
    curFigure = 'C';
}

void Widget::onRectanglebuttonClicked()
{
    curFigure = "R";
}

void Widget::onRotateToLeftButtonClicked()
{
    QList<QGraphicsItem*> allItems = scene->items();
    QList<QGraphicsItem*> list = scene->selectedItems();
    for (int i = 0; i < list.size(); i++) {
        list[i]->setRotation(list[i]->rotation() - 5);
        for (int j = 0; j < allItems.size(); j++) {
            if (c.getShapeAt(j)->x == list[i]->x() && c.getShapeAt(j)->y == list[i]->y()) {
                c.getShapeAt(j)->rotat = list[i]->rotation();
            }
        }
    }
}

void Widget::onRotateToRightButtonClicked()
{

    QList<QGraphicsItem*> allItems = scene->items();
    QList<QGraphicsItem*> list = scene->selectedItems();
    for (int i = 0; i < list.size(); i++) {
        list[i]->setRotation(list[i]->rotation() + 5);
        for (int j = 0; j < allItems.size(); j++) {
            if (c.getShapeAt(j)->x == list[i]->x() && c.getShapeAt(j)->y == list[i]->y()) {
                c.getShapeAt(j)->rotat = list[i]->rotation();
            }
        }
    }
}

void Widget::drowCircle(const QPoint & pos, double rot)
{
    int x = pos.x();
    int y = pos.y();
    Shape* tmp = new Circle();
    tmp->figType = 'C';
    tmp->x = x;
    tmp->y = y;
    tmp->rotat = rot;
    if (addShapeToContainer) {
        c.add(tmp);
    }
    QGraphicsEllipseItem* item = scene->addEllipse(-10, -10, 20, 20, QPen(Qt::blue, 1, Qt::SolidLine), QBrush(Qt::blue));
    item->setFlag(QGraphicsItem::ItemIsMovable, true);
    item->setFlag(QGraphicsItem::ItemIsSelectable, true);
    item->setPos(x,y);
    item->setRotation(rot);
}

void Widget::drowSquare(const QPoint &pos, double rot)
{
    int x = pos.x();
    int y = pos.y();
    Shape* tmp = new Square();
    tmp->figType = 'S';
    tmp->x = x;
    tmp->y = y;
    tmp->rotat = rot;
    if (addShapeToContainer) {
        c.add(tmp);
    }
    QGraphicsRectItem* item = scene->addRect(-15, -15, 30, 30, QPen(Qt::red, 1, Qt::SolidLine), QBrush(Qt::red));
    item->setFlag(QGraphicsItem::ItemIsMovable, true);
    item->setFlag(QGraphicsItem::ItemIsSelectable, true);
    item->setPos(x,y);
    item->setRotation(rot);
}

void Widget::drowRectangle(const QPoint &pos, double rot)
{
    int x = pos.x();
    int y = pos.y();
    Shape* tmp = new Rectangle();
    tmp->figType = 'R';
    tmp->x = x;
    tmp->y = y;
    tmp->rotat = rot;
    if (addShapeToContainer) {
        c.add(tmp);
    }
    QGraphicsRectItem* item = scene->addRect(-30, -15, 60, 30, QPen(Qt::green, 1, Qt::SolidLine), QBrush(Qt::green));
    item->setFlag(QGraphicsItem::ItemIsMovable, true);
    item->setFlag(QGraphicsItem::ItemIsSelectable, true);
    item->setPos(x,y);
    item->setRotation(rot);
}

void Widget::mousePressEvent(QMouseEvent * ev)
{
    if (ev->button() == Qt::LeftButton) {
        addShapeToContainer = true;
        if (curFigure == "C") {
            drowCircle(ev->pos(), 0);
        }
        if(curFigure == "S") {
            drowSquare(ev->pos(), 0);
        }
        if (curFigure == "R") {
            drowRectangle(ev->pos(), 0);
        }
    }

}

void Widget::showAllShapes()
{
    addShapeToContainer = false;
    for (int i = 0; i < c.getSize(); i++) {
        QPoint pos;
        pos.setX(c.getShapeAt(i)->x);
        pos.setY(c.getShapeAt(i)->y);
        if (c.getShapeAt(i)->figType == 'C') {
            drowCircle(pos, c.getShapeAt(i)->rotat);
        }
        if (c.getShapeAt(i)->figType == 'R') {
            drowRectangle(pos, c.getShapeAt(i)->rotat);
        }
        if (c.getShapeAt(i)->figType == 'S') {
            drowSquare(pos, c.getShapeAt(i)->rotat);
        }
    }
}
